

init();
function init() {
    //1.查询进度列表
    queryPorjectList();
    queryWarehouseList();
}

/**
 * 详情
 */
function queryWarehouseDetail(id) {
    $.ajax({
        url :   "warehouse/warehouseDetail",
        type:   "post",
        data:JSON.stringify({
            /*  pageIndex : pageIndex,*/
            warehouseId : id
        }),
        contentType:"application/json;",
        success:function(data){
            console.log(data);
            goToLogin(data);
            var list = data.data.list;
            console.log(list);
            var warehouseDetailHtml  = '<thead>'
                +'<tr>'
                +'<th>材料名称</th>'
                +'<th>数量</th>'
                +'<th>类型名称</th>'
                +'<th>材料備注</th>'
                +'<th>操作類型</th>'
                +'</tr>'
                +'</thead>'
                +'<tbody>';
            for(var i=0; i<list.length; i++){
                var itpye="";
                if (list[i].state==1){
                    itpye="入库";
                }else{
                    itpye="出库";
                }
                warehouseDetailHtml = warehouseDetailHtml
                    +'<tr>'
                    +'<td>'
                    +list[i].materialName
                    +'</td>'
                    +'<td>'
                    +list[i].materialNumber
                    +'</td>'
                    +'<td>'
                    +list[i].materialTypeName
                    +'</td>'
                    +'<td>'
                    +list[i].materialDesc
                    +'</td>'
                    +'<td>'
                    +itpye
                    +'</td>'
                    +'</tr>';
            }
            warehouseDetailHtml = warehouseDetailHtml + +'</tbody>';
            $("#warehouseDetailList").html(warehouseDetailHtml);

        }
    });
}

function queryPorjectList(pageIndex) {
    $.ajax({
        url: prefixUrl + "project/queryList",
        type: "post",
        data: JSON.stringify({pageIndex:pageIndex}),
        contentType: "application/json;",
        success: function (data) {
            var projectHtml = "";
            projectHtml ='<option value=0>请选择项目</option>';
            var list = data.data.rows;
            for(var i=0; i<list.length; i++){
                projectHtml = projectHtml
                    +'<option style="text-align: center;" value="'
                    +list[i].projectId
                    +'">'
                    +list[i].projectName
                    +'</option>';
            }
            $("#projectList").html(projectHtml);
        }
    });
}



/**
 * 仓库列表
 */
function queryWarehouseList(pageIndex) {
    var state = "";
    state = $("#sel").val();
    var projectId=$("#projectList").val();
    if(pageIndex == null){
        pageIndex = 0;
    }
    $.ajax({
        url :   prefixUrl + "warehouse/getwarehousebystate",
        type:   "post",
        data:JSON.stringify({
            pageIndex : pageIndex,
            state:state,
            projectId:projectId,
        }),
        contentType:"application/json;",
        success:function(data){
            goToLogin(data);
            var list = data.data.rows;
            var warehouseHtml  = '<thead>'
                +'<tr>'
                +'<th>序号</th>'
                +'<th>项目名称</th>'
                +'<th>材料备注</th>'
                +'<th>申请人姓名</th>'
                +'<th>操作/申请时间</th>'
                +'<th>类型</th>'
                +'<th>操作</th>'
                +'</tr>'
                +'</thead>'
                +'<tbody>';
            for(var i=0; i<list.length; i++){
                var desc = list[i].materialDesc;if(desc.length > 30){ desc =  desc.substr(0,29) + "..."}
                var typeHtml = "";
                var state = list[i].state;
                if(state == 1){
                    typeHtml = '<span style="color: green">入库</span>';
                }else{
                    typeHtml = '<span style="color: red">出库</span>';
                }

                warehouseHtml = warehouseHtml
                    +'<tr>'
                    +'<td>' + (parseInt(i) + 1) + '</td>'
                    +'<td>'
                    +list[i].projectName
                    +'</td>'
                    +'<td>'
                    +desc
                    +'</td>'
                    +'<td>'
                    +list[i].applyName
                    +'</td>'
                    +'<td>'
                    +new Date(list[i].oprDate).toLocaleDateString()
                    +'</td>'
                    +'<td>'
                    +typeHtml
                    +'</td>'
                    +'<td>'
                    +'<button type="button" class="btn btn-info mar-r-5" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo" onclick=queryWarehouseDetail("'
                    + list[i].warehouseId
                    +'")><i class="glyphicon glyphicon-search"></i></button>'
                    +'<button type="button" class="btn btn-info" onclick=delteWarehouseInfo("'
                    +list[i].warehouseId
                    +'")><i class="fa fa-bitbucket-square"></i></button>'
                    +'</td>'
                    +'</tr>';
            }
            warehouseHtml = warehouseHtml + +'</tbody>';
            $("#warehouseList").html(warehouseHtml);



            var total = data.data.total;
            var remainder = total%20;
            var allNumber = 0;
            if(remainder > 0){
                allNumber = parseInt(total/20) + 1;
            }else{
                allNumber = parseInt(total/20);
            }
            var number = 1;
            var numberHtml = '<li><a onclick=queryWarehouseList('+ "0" +')>«</a></li>';
            for(var i=0; i<allNumber; i++){
                var id = "query-" + i;
                if(i == 0){
                    numberHtml = numberHtml
                        +'<li id="'+ id + '" name="queryList" class="active"><a onclick=queryWarehouseList('+ i +')>' + number + '</a></li>';
                }else{
                    numberHtml = numberHtml
                        +'<li id="'+ id + '" name="queryList"><a onclick=queryWarehouseList('+ i +')>' + number + '</a></li>';
                }
                number++;
            }
            numberHtml = numberHtml + '<li><a onclick=queryWarehouseList('+ (allNumber-1) +')>»</a></li>';
            $("#queryList").html(numberHtml);
        }
    });
}

function delteWarehouseInfo(id) {
    if(confirm("确定要进行删除嘛?")){
        $.ajax({
            url :   prefixUrl + "warehouse/deletewarehouse",
            type:   "post",
            data:JSON.stringify({
                warehouseId:id
            }),
            contentType:"application/json;",
            success:function(data){
                goToLogin(data);
                alert(data.msg);
                queryWarehouseList(0);
            }
        });
    }
}